<?php
class wap_delivery_process{

    //发货
    function consignDelivery($params)
    {
        $deliveryObj    = app::get('wap')->model('delivery');

        #更新校验码状态
        $wapDeliveryCodeObj    = app::get('wap')->model('delivery_code');
        $wapDeliveryCodeObj->update(array('status'=>1), array('delivery_bn'=>$params['delivery_bn']));
        
        #wap发货单更新
        $dlydata    = array();
        $delivery_time    = time();
        
        $dlydata['delivery_id'] = $params['delivery_id'];
        $dlydata['status'] = 3;
        $dlydata['process_status'] = 7;
        $dlydata['delivery_time'] = $delivery_time;
        $deliveryObj->save($dlydata);

        //Wap发货单发货触发通知OMS发货
        $wms_id    = kernel::single('ome_branch')->getWmsIdById($params['branch_id']);
        
        $request_params = array(
                'delivery_bn' => $params['outer_delivery_bn'],
                'delivery_time' => time(),
                'weight' => $params['weight'],
                'delivery_cost_actual' => $params['delivery_cost_actual'],
                'logi_id' => $params['logi_id'],
                'logi_no' => $params['outer_delivery_bn'],
        );
        $res    = kernel::single('wap_event_trigger_delivery')->consign($wms_id, $request_params, true);
        
        return true;
    }

    //门店确认（包括门店自提和门店配送）
    function accept($params){
        
        //确认接收该门店门店自提单或门店配送
        $deliveryObj    = app::get('wap')->model('delivery');
        $update_data    = array('delivery_id'=>$params['delivery_id'], 'confirm'=>1);
        $deliveryObj->save($update_data);

        //获取仓库对应的仓储类型
        $wms_id    = kernel::single('ome_branch')->getWmsIdById($params['branch_id']);

        #获取发货仓库对应的门店店铺信息
        $wapDeliveryLib    = kernel::single('wap_delivery');
        $branchShopInfo    = $wapDeliveryLib->getBranchShopInfo($params['branch_id']);
        
        //获取是门店自提还是门店配送
        $corpTypeLib = kernel::single('o2o_corp_type');
        $dlyCorpInfo = $corpTypeLib->get_corp_type($params["logi_id"],true);
        
        //判断是否开启销单校验码 关闭状态下$pickup_code不生成不赋值
        if(app::get('o2o')->getConf('o2o.delivery.confirm.code') == "true"){
            #生成六位提货单的校验码
            $wapCodeLib = kernel::single('wap_code');
            $pickup_code = $wapCodeLib->gen_code($params['delivery_bn']);
        }
        
        //订单确认参数组织 门店配送短信模板只需要ship_mobile ship_name store_name store_contact_tel
        $request_params    = array(
                'dly_corp_type' => $dlyCorpInfo["type"],
                'delivery_bn' => $params['outer_delivery_bn'],
                'pickup_bn' => $params['delivery_bn'],
                'pickup_code' => $pickup_code,
                'ship_mobile' => $params['consignee']['mobile'],
                'ship_name' => $params['consignee']['name'],
                'store_name' => $branchShopInfo['store_name'],#门店名称
                'store_contact_tel' => $branchShopInfo['tel'] ? $branchShopInfo['tel'] : $branchShopInfo['mobile'],#门店联系电话
        );
        
        //门店自提单
        if($dlyCorpInfo["type"] == "o2o_pickup"){
            //门店自提短信还需要门店地址
            $request_params["store_addr"] = $branchShopInfo['addr'];
        }
        
        $res    = kernel::single('wap_event_trigger_delivery')->confirm($wms_id, $request_params, true);
        return true;
    }

    //门店自提拒绝
    function refuse($params){

        $deliveryObj    = app::get('wap')->model('delivery');

        //门店自提单拒绝打回
        $update_data    = array('delivery_id'=>$params['delivery_id'], 'confirm'=>2);
        $deliveryObj->save($update_data);

        //获取仓库对应的仓储类型
        $wms_id    = kernel::single('ome_branch')->getWmsIdById($params['branch_id']);

        $request_params = array(
            'delivery_bn' => $params['outer_delivery_bn'],
            'memo' => '门店拒绝',
        );

        $res = kernel::single('wap_event_trigger_delivery')->reback($wms_id, $request_params, true);
        return true;
    }

    //重发提货校验码短信
    function reSendMsg($params){

        $deliveryObj    = app::get('wap')->model('delivery');

        //获取仓库对应的仓储类型
        $wms_id    = kernel::single('ome_branch')->getWmsIdById($params['branch_id']);

        #获取发货仓库对应的门店店铺信息
        $wapDeliveryLib    = kernel::single('wap_delivery');
        $branchShopInfo    = $wapDeliveryLib->getBranchShopInfo($params['branch_id']);
        
        //获取是门店自提还是门店配送
        $corpTypeLib = kernel::single('o2o_corp_type');
        $dlyCorpInfo = $corpTypeLib->get_corp_type($params["logi_id"],true);

        //判断是否开启销单校验码 关闭状态下$pickup_code不生成不赋值
        if(app::get('o2o')->getConf('o2o.delivery.confirm.code') == "true"){
            #生成六位提货单的校验码
            $wapCodeLib = kernel::single('wap_code');
            //作废老的校验码
            $wapCodeLib->del_code($params['delivery_bn']);
            //生成六位提货单的校验码
            $pickup_code = $wapCodeLib->gen_code($params['delivery_bn']);
        }
        
        //订单确认参数组织
        $request_params    = array(
            'dly_corp_type' => $dlyCorpInfo["type"],
            'delivery_bn' => $params['outer_delivery_bn'],
            'pickup_bn' => $params['delivery_bn'],
            'pickup_code' => $pickup_code,
            'ship_mobile' => $params['consignee']['mobile'],
            'ship_name' => $params['consignee']['name'],
            'store_name' => $branchShopInfo['store_name'],#门店名称
            'store_contact_tel' => $branchShopInfo['tel'] ? $branchShopInfo['tel'] : $branchShopInfo['mobile'],#门店联系电话
        );

        //门店自提单
        if($dlyCorpInfo["type"] == "o2o_pickup"){
            $request_params["store_addr"] = $branchShopInfo['addr'];
        }

        $res    = kernel::single('wap_event_trigger_delivery')->confirm($wms_id, $request_params, true);
        return true;
    }
}
